﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Xml;
using Autodesk.Revit.DB;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;

namespace YArchitech.Plumbing
{
	public partial class FormSystemSetDetailed : System.Windows.Forms.Form
	{
		public FormSystemSetDetailed(Document doc)
		{
			this.InitializeComponent();
			YJKProcessTools hyprocessTools = new YJKProcessTools();
			hyprocessTools.ShowInTaskbarChange(this);
			this.doc = doc;
			base.CenterToScreen();
			this.comboBoxSelectSystem.Items.Add("建筑");
			this.comboBoxSelectSystem.Items.Add("暖通风系统");
			this.comboBoxSelectSystem.Items.Add("暖通水系统");
			this.comboBoxSelectSystem.Items.Add("消防");
			this.comboBoxSelectSystem.Items.Add("给排水");
			this.comboBoxSelectSystem.SelectedItem = "建筑";
			this.formCheckBoxList.Add(this.checkBoxPipe);
			this.formCheckBoxList.Add(this.checkBoxCeilings);
			this.formCheckBoxList.Add(this.checkBoxColumns);
			this.formCheckBoxList.Add(this.checkBoxDoors);
			this.formCheckBoxList.Add(this.checkBoxDuct);
			this.formCheckBoxList.Add(this.checkBoxDuctAccessory);
			this.formCheckBoxList.Add(this.checkBoxDuctFitting);
			this.formCheckBoxList.Add(this.checkBoxDuctTerminal);
			this.formCheckBoxList.Add(this.checkBoxFloors);
			this.formCheckBoxList.Add(this.checkBoxMechanicalEquipment);
			this.formCheckBoxList.Add(this.checkBoxOST_PipeFitting);
			this.formCheckBoxList.Add(this.checkBoxPipeAccessory);
			this.formCheckBoxList.Add(this.checkBoxSprinklers);
			this.formCheckBoxList.Add(this.checkBoxWalls);
			this.formCheckBoxList.Add(this.checkBoxWindows);
		}

		private void buttonOK_Click(object sender, EventArgs e)
		{
			this.writeUserSetToXmlButtonOk("Archtecture", "建筑", this.archetureList);
			this.writeUserSetToXmlButtonOk("DuctSystem", "暖通风系统", this.mepDuctList);
			this.writeUserSetToXmlButtonOk("PipeSystem", "暖通水系统", this.mepWaterList);
			this.writeUserSetToXmlButtonOk("FireSystem", "消防", this.fireList);
			this.writeUserSetToXmlButtonOk("WaterSystem", "给排水", this.waterList);
			base.Close();
		}

		private void writeUserSetToXmlButtonOk(string str, string systemName, List<string> list)
		{
			if (list.Count > 0 && this.comboBoxSelectSystem.SelectedItem.ToString() != systemName)
			{
				this.writeUserSetToXml(str, list);
				return;
			}
			if (list.Count > 0 && this.comboBoxSelectSystem.SelectedItem.ToString() == systemName)
			{
				this.addInfomationToList(list);
				this.writeUserSetToXml(str, list);
				return;
			}
			if (list.Count == 0 && this.comboBoxSelectSystem.SelectedItem.ToString() == systemName)
			{
				this.addInfomationToList(list);
				this.writeUserSetToXml(str, list);
			}
		}

		private SystemSetStruct[] read(string readNodeName)
		{
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(SystemSet.path);
			XmlNodeList childNodes = xmlDocument.SelectSingleNode("configuration").ChildNodes;
			XmlNodeList xmlNodeList = null;
			foreach (object obj in childNodes)
			{
				XmlNode xmlNode = (XmlNode)obj;
				XmlElement xmlElement = (XmlElement)xmlNode;
				if (xmlElement.Attributes["name"].Value == readNodeName)
				{
					xmlNodeList = xmlElement.ChildNodes;
					break;
				}
			}
			int count = xmlNodeList.Count;
			SystemSetStruct[] array = new SystemSetStruct[count];
			for (int i = 0; i < count; i++)
			{
				array[i] = new SystemSetStruct();
			}
			int num = 0;
			foreach (object obj2 in xmlNodeList)
			{
				XmlNode xmlNode2 = (XmlNode)obj2;
				XmlElement xmlElement2 = (XmlElement)xmlNode2;
				if (num < count)
				{
					array[num].CategoryName = (BuiltInCategory)Enum.Parse(typeof(BuiltInCategory), xmlElement2.Attributes["BuiltInCategory"].Value, true);
					array[num].CheckBox = (CheckBox)this.aboutNameFindControl(xmlElement2.Name);
					num++;
				}
			}
			return array;
		}

        private System.Windows.Forms.Control aboutNameFindControl(string name)
		{
			object value = base.GetType().GetField(name, BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.NonPublic).GetValue(this);
            return (System.Windows.Forms.Control)value;
		}

		private void writeUserSetToXml(string readNodeName, List<string> list)
		{
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(SystemSet.path);
			XmlNodeList childNodes = xmlDocument.SelectSingleNode("configuration").ChildNodes;
			XmlNodeList xmlNodeList = null;
			XmlElement xmlElement = null;
			foreach (object obj in childNodes)
			{
				XmlNode xmlNode = (XmlNode)obj;
				XmlElement xmlElement2 = (XmlElement)xmlNode;
				if (xmlElement2.Attributes["name"].Value == readNodeName)
				{
					xmlElement2.Attributes["systemClass"].Value = list.ElementAt(0);
					xmlElement2.Attributes["startLevel"].Value = list.ElementAt(1);
					xmlElement2.Attributes["endLevel"].Value = list.ElementAt(2);
					xmlElement = xmlElement2;
					xmlNodeList = xmlElement.ChildNodes;
					break;
				}
			}
			List<XmlNode> list2 = new List<XmlNode>();
			foreach (object obj2 in xmlNodeList)
			{
				XmlNode item = (XmlNode)obj2;
				list2.Add(item);
			}
			foreach (XmlNode oldChild in list2)
			{
				xmlElement.RemoveChild(oldChild);
			}
			int num = 0;
			foreach (string text in list)
			{
				if (num > 2)
				{
					XmlNode xmlNode2 = xmlDocument.CreateElement(text);
					(xmlNode2 as XmlElement).SetAttribute("BuiltInCategory", this.getUseNodeName(text));
					xmlElement.AppendChild(xmlNode2);
				}
				num++;
			}
			xmlDocument.Save(SystemSet.path);
		}

		private string getUseNodeName(string nodeName)
		{
			string result = null;
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(SystemSet.path);
			XmlNodeList childNodes = xmlDocument.SelectSingleNode("configuration").ChildNodes;
			XmlNodeList xmlNodeList = null;
			foreach (object obj in childNodes)
			{
				XmlNode xmlNode = (XmlNode)obj;
				XmlElement xmlElement = (XmlElement)xmlNode;
				if (xmlElement.Attributes["name"].Value == "CheckBoxData")
				{
					xmlNodeList = xmlElement.ChildNodes;
				}
			}
			foreach (object obj2 in xmlNodeList)
			{
				XmlNode xmlNode2 = (XmlNode)obj2;
				XmlElement xmlElement2 = (XmlElement)xmlNode2;
				if (xmlElement2.Name == nodeName)
				{
					result = xmlElement2.Attributes["BuiltInCategory"].Value;
				}
			}
			return result;
		}

		private List<string> getCheckedBoxTrue()
		{
			List<string> list = new List<string>();
			foreach (CheckBox checkBox in this.formCheckBoxList)
			{
				if (checkBox.Checked)
				{
					list.Add(checkBox.Name);
				}
			}
			return list;
		}

		private void buttonSystemClass_Click(object sender, EventArgs e)
		{
			if (this.comboBoxSelectSystem.SelectedItem.ToString() != "建筑")
			{
                System.Windows.Forms.Form form = new FormSystemClass(this.textBoxSystemClass);
				form.ShowDialog();
				return;
			}
			YJKMessageBox.Show("建筑里面没有系统分类！");
		}

		private string judgeComboBoxSelectedItemGetNodeName()
		{
			string result = "";
			if (this.comboBoxSelectSystem.SelectedItem.ToString() == "建筑")
			{
				result = "Archtecture";
			}
			else if (this.comboBoxSelectSystem.SelectedItem.ToString() == "暖通风系统")
			{
				result = "DuctSystem";
			}
			else if (this.comboBoxSelectSystem.SelectedItem.ToString() == "暖通水系统")
			{
				result = "PipeSystem";
			}
			else if (this.comboBoxSelectSystem.SelectedItem.ToString() == "消防")
			{
				result = "FireSystem";
			}
			else if (this.comboBoxSelectSystem.SelectedItem.ToString() == "给排水")
			{
				result = "WaterSystem";
			}
			return result;
		}

		private void comboBoxSelectSystem_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (this.comboBoxSelectSystem.SelectedItem.ToString() == "建筑")
			{
				this.buttonSystemClass.Enabled = false;
				this.textBoxStartLevel.ReadOnly = true;
				this.textBoxEndLevel.ReadOnly = true;
			}
			else
			{
				this.buttonSystemClass.Enabled = true;
				this.textBoxStartLevel.ReadOnly = false;
				this.textBoxEndLevel.ReadOnly = false;
			}
			string systemClassName = this.comboBoxSelectSystem.SelectedItem.ToString();
			foreach (CheckBox checkBox in this.formCheckBoxList)
			{
				checkBox.Checked = false;
			}
			List<SystemSetStruct> list = new List<SystemSetStruct>();
			list.AddRange(this.read(this.judgeComboBoxSelectedItemGetNodeName()));
			if (!this.judgeReadXmlOrList(systemClassName))
			{
				foreach (SystemSetStruct systemSetStruct in list)
				{
					systemSetStruct.CheckBox.Checked = true;
				}
			}
			if (this.judgeReadXmlOrList(systemClassName))
			{
				int num = 0;
				foreach (string name in this.accordSystemNameGetList(systemClassName))
				{
					if (num > 2)
					{
						(this.aboutNameFindControl(name) as CheckBox).Checked = true;
					}
					num++;
				}
			}
			this.textBoxSystemClass.Text = "";
			this.textBoxStartLevel.Text = "";
			this.textBoxEndLevel.Text = "";
			List<string> source = SystemSetHelper.readSystemClassAndLevelInfo(this.judgeComboBoxSelectedItemGetNodeName());
			if (!this.judgeReadXmlOrList(systemClassName))
			{
				this.textBoxSystemClass.Text = source.ElementAt(0);
				this.textBoxStartLevel.Text = source.ElementAt(1);
				this.textBoxEndLevel.Text = source.ElementAt(2);
			}
			if (this.judgeReadXmlOrList(systemClassName))
			{
				this.textBoxSystemClass.Text = this.accordSystemNameGetList(systemClassName).ElementAt(0);
				this.textBoxStartLevel.Text = this.accordSystemNameGetList(systemClassName).ElementAt(1);
				this.textBoxEndLevel.Text = this.accordSystemNameGetList(systemClassName).ElementAt(2);
			}
			this.nowSystemName = this.comboBoxSelectSystem.SelectedItem.ToString();
		}

		private void buttonESC_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private bool judgeReadXmlOrList(string systemClassName)
		{
			List<string> list = null;
			if (systemClassName == "建筑")
			{
				list = this.archetureList;
			}
			else if (systemClassName == "暖通风系统")
			{
				list = this.mepDuctList;
			}
			else if (systemClassName == "暖通水系统")
			{
				list = this.mepWaterList;
			}
			else if (systemClassName == "消防")
			{
				list = this.fireList;
			}
			else if (systemClassName == "给排水")
			{
				list = this.waterList;
			}
			bool result = false;
			if (list != null && list.Count > 0)
			{
				result = true;
			}
			return result;
		}

		private List<string> accordSystemNameGetList(string systemClassName)
		{
			List<string> result = null;
			if (systemClassName == "建筑")
			{
				result = this.archetureList;
			}
			else if (systemClassName == "暖通风系统")
			{
				result = this.mepDuctList;
			}
			else if (systemClassName == "暖通水系统")
			{
				result = this.mepWaterList;
			}
			else if (systemClassName == "消防")
			{
				result = this.fireList;
			}
			else if (systemClassName == "给排水")
			{
				result = this.waterList;
			}
			return result;
		}

		private void addInfomationToList(List<string> list)
		{
			list.Clear();
			list.Add(this.textBoxSystemClass.Text);
			list.Add(this.textBoxStartLevel.Text);
			list.Add(this.textBoxEndLevel.Text);
			list.AddRange(this.getCheckedBoxTrue());
		}

		private void buttonInput_Click(object sender, EventArgs e)
		{
		}

		private void buttonOutPut_Click(object sender, EventArgs e)
		{
		}

		private void comboBoxSelectSystem_SelectionChangeCommitted(object sender, EventArgs e)
		{
			if (this.nowSystemName == "建筑")
			{
				this.addInfomationToList(this.archetureList);
			}
			if (this.nowSystemName == "暖通风系统")
			{
				this.addInfomationToList(this.mepDuctList);
			}
			if (this.nowSystemName == "暖通水系统")
			{
				this.addInfomationToList(this.mepWaterList);
			}
			if (this.nowSystemName == "消防")
			{
				this.addInfomationToList(this.fireList);
			}
			if (this.nowSystemName == "给排水")
			{
				this.addInfomationToList(this.waterList);
			}
		}

		private void textBoxStartLevel_Validated(object sender, EventArgs e)
		{
			if (this.comboBoxSelectSystem.SelectedItem.ToString() != "建筑")
			{
				Regex regex = new Regex("[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?");
				if (!regex.IsMatch(this.textBoxStartLevel.Text))
				{
					YJKMessageBox.Show("起始标高只能输入正负小数或整数！");
					this.textBoxStartLevel.Focus();
					this.textBoxStartLevel.SelectAll();
				}
			}
		}

		private void textBoxEndLevel_Validated(object sender, EventArgs e)
		{
			if (this.comboBoxSelectSystem.SelectedItem.ToString() != "建筑")
			{
				Regex regex = new Regex("[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?");
				if (!regex.IsMatch(this.textBoxEndLevel.Text))
				{
					YJKMessageBox.Show("结束标高只能输入正负小数或整数！");
					this.textBoxEndLevel.Focus();
					this.textBoxEndLevel.SelectAll();
				}
			}
		}

		private Document doc;

		private List<CheckBox> formCheckBoxList = new List<CheckBox>();

		private SystemSetHelper helper = new SystemSetHelper();

		private string nowSystemName = "建筑";

		private List<string> archetureList = new List<string>();

		private List<string> mepDuctList = new List<string>();

		private List<string> mepWaterList = new List<string>();

		private List<string> fireList = new List<string>();

		private List<string> waterList = new List<string>();

		private List<XmlNode> nodeList = new List<XmlNode>();
	}
}
